package code.structure.manager.mouse
{
	import code.main.App;
	import code.main.AppCommonView;
	import code.structure.manager.loader.DllLoaderManager;
	import code.untils.MouseCursorConst;
	import code.untils.UiNameConst;
	import code.structure.manager.bitmap.YDisplayConvert;
	
	import com.patience.mvc.context.ContextIntance;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.utils.Dictionary;

	/**
	 *鼠标管理器 
	 * @author Yuri
	 * 
	 */
	public class MouseManager
	{
		private static var _instance:MouseManager;
		
		/**
		 *鼠标样式容器 
		 */
		private var _cursorContainer:Sprite;
		/**
		 *正常鼠标样式 
		 */
		private var _normalMouseCursor:Bitmap;
		/**
		 *跟随鼠标样式 
		 */
		private var _followMouseCursor:Bitmap;
		/**
		 *鼠标bitmapdata集合 
		 */
		private var _mouseCursorBitMapDataDic:Dictionary;
		private var _cursorStyle:String;
		private var _followCursorStyle:String;
		public function MouseManager()
		{
			if(_instance){ 
				throw new Error("实例化单例类出错-MouseManager");
			}
		}
		
		public static function getInstance():MouseManager{
			if(!_instance) _instance = new MouseManager();
			return _instance;
		}
		
		public function init():void{
			_mouseCursorBitMapDataDic = new Dictionary();
			_cursorContainer = new Sprite();
			
			(ContextIntance.getContext().commonView as AppCommonView).appContainer.mouseLayer.addChild(_cursorContainer);
			
//			_followMouseCursor.scaleX = _followMouseCursor.scaleY = .6;
		/*	Mouse.hide();
			_normalMouseCursor = new Bitmap(null,"auto",true);
			_cursorContainer.addChild(_normalMouseCursor);
			cursorStyle = MouseCursorConst.CURSOR_NORMAL;*/
			
			App.appstage.addEventListener(Event.ENTER_FRAME,onMouseMoveHandler);
//			App.appstage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveHandler);
		}
		
		/**
		 *创建跟随鼠标样式 
		 * 
		 */
		private function createFollowMouseCursor():void{
			_followMouseCursor = new Bitmap(null,"auto",true);
			_cursorContainer.addChild(_followMouseCursor);
			if(_normalMouseCursor){
				_followMouseCursor.x = _normalMouseCursor.width-4;
				_followMouseCursor.y = _normalMouseCursor.height-4;
			}else{
				_followMouseCursor.x = 10;
				_followMouseCursor.y = 15;
			}
		}
		protected function onMouseMoveHandler(event:*):void
		{
			_cursorContainer.x = App.appstage.mouseX;
			_cursorContainer.y = App.appstage.mouseY;
		}
		
		/**
		 *清除鼠标样式 
		 * 
		 */
		public function clearCursor():void{
			if(_normalMouseCursor)
				_normalMouseCursor.bitmapData = null;
			_cursorStyle = null;
			Mouse.show();
		}
		
		/**
		 *清除跟随鼠标 
		 * 
		 */
		public function clearFollowCursor():void{
			if(_followMouseCursor){
				_followMouseCursor.bitmapData = null;
				if(_cursorContainer.contains(_followMouseCursor)){
					_cursorContainer.removeChild(_followMouseCursor);
				}
			}
			_followMouseCursor = null;
			_followCursorStyle = null;
		}

		/**
		 *光标样式 
		 */
		public function get cursorStyle():String
		{
			return _cursorStyle;
		}

		/**
		 * @private
		 */
		public function set cursorStyle(value:String):void
		{
			//return ;//暂时没用
			if(cursorStyle == value) return;
			_cursorStyle = value;
			if(_cursorStyle == MouseCursorConst.CURSOR_NORMAL){
				if(_mouseCursorBitMapDataDic[_followCursorStyle]){
					_normalMouseCursor.bitmapData = _mouseCursorBitMapDataDic[_cursorStyle];
				}else{
					dealMouseCursorData(UiNameConst.GENERAL_ICON,_cursorStyle,_cursorStyle);
					_normalMouseCursor.bitmapData = _mouseCursorBitMapDataDic[_cursorStyle];
				}
				if(!_normalMouseCursor.bitmapData) clearCursor();
			}else{
				clearCursor();
			}
		}
		
		/**
		 *鼠标跟随改变 
		 */
		public function set followCursorStyle(value:String):void{
			if(!value){
				clearFollowCursor();
				return;
			}
			if(_followCursorStyle == value) return;
			if(!_followMouseCursor) createFollowMouseCursor();
			_followCursorStyle = value;
			if(_mouseCursorBitMapDataDic[_followCursorStyle]){
				_followMouseCursor.bitmapData = _mouseCursorBitMapDataDic[_followCursorStyle];
			}else{
				dealMouseCursorData(UiNameConst.GENERAL_ICON,_followCursorStyle,_followCursorStyle);
				_followMouseCursor.bitmapData = _mouseCursorBitMapDataDic[_followCursorStyle] || null;
			}
		}
		
		private function dealMouseCursorData(key:String,subKey:String,cStyle:String):void{
			var cursorClass:Class = DllLoaderManager.getInstance().getMaterial(key,subKey) as Class;
			if(!cursorClass){
				_mouseCursorBitMapDataDic[cStyle] = null;
				return;
			}
//			var bit:BitmapData = new cursorClass as BitmapData;
			var bit:BitmapData;
			bit = YDisplayConvert.drawBitMapDataByClass(cursorClass);
			if(bit){
				_mouseCursorBitMapDataDic[cStyle] = bit;
			}else{
				_mouseCursorBitMapDataDic[cStyle] = null;
			}
		}

		/**
		 *是否显示跟随鼠标样式 
		 * @param isShow
		 * 
		 */
		public function displayFollowMouseCuersor(isShow:Boolean=true):void{
			if(!_followMouseCursor) return;
			_followMouseCursor.visible = isShow;
		}
	}
}